package com.burgeon.backend.SHOPBACKEND.controller;

import com.burgeon.backend.SHOP.service.SystemService;
import com.burgeon.backend.Util.menu.MenuManager;
import com.burgeon.backend.Util.menu.WeChatInfo;
import com.burgeon.backend.Util.menu.WeixinUtil;
import com.burgeon.backend.Util.wechatUtil.WeGetAccessToken;
import com.burgeon.backend.WeChat.GetAccesToken;
import com.burgeon.backend.stsutil.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin
@RequestMapping("menu")
public class WxMenuController extends BaseController{
    @Autowired
    private MenuManager menuManager;
    private static Logger log = LoggerFactory.getLogger(MenuManager.class);

    @Autowired
    private GetAccesToken getAccesToken;

    @Autowired
    private SystemService systemService;

    @ResponseBody
    @RequestMapping(value = "/createMenu")
    public ResponseResult<String> CreateMenu(){
        // 第三方用户唯一凭证
        String appId = "你公众号的appId";
        // 第三方用户唯一凭证密钥
        String appSecret = "你公众号的appSecret";

        // 调用接口获取access_token
       // String at = WeGetAccessToken.getAccessToken();
        //String accessToken = getAccesToken.GetAccesToken(WeChatInfo.token);
        String accessToken = systemService.GetAccesToken(WeChatInfo.token);

        if (null != accessToken) {
            // 调用接口创建菜单
            String result = WeixinUtil.createMenu(MenuManager.getMenu(), accessToken);
            //String result =""; //WeixinUtil.deleteMenu(getMenu(),at);
            System.err.println(result);
            // 判断菜单创建结果
            if ("0".equals(result))
                log.info("菜单创建成功！");
            else
                log.info("菜单创建失败，错误码：" + result);
            return new ResponseResult<>(SUCCESS,result);
        }
       return null;
    }

}
